package net.maku.propagate.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.mybatis.service.impl.BaseServiceImpl;
import net.maku.framework.security.user.SecurityUser;
import net.maku.propagate.convert.PropagateNewsConvert;
import net.maku.propagate.entity.PropagateNewsEntity;
import net.maku.propagate.enums.ApplyEnum;
import net.maku.propagate.query.PropagateNewsQuery;
import net.maku.propagate.vo.PropagateNewsVO;
import net.maku.propagate.dao.PropagateNewsDao;
import net.maku.propagate.service.PropagateNewsService;
import net.maku.system.service.SysOrgService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 新闻资讯
 *
 * @author 阿沐 babamu@126.com
 * @since 1.0.0 2025-03-16
 */
@Service
@AllArgsConstructor
public class PropagateNewsServiceImpl extends BaseServiceImpl<PropagateNewsDao, PropagateNewsEntity> implements PropagateNewsService {

    private final SysOrgService sysOrgService;

    @Override
    public PageResult<PropagateNewsVO> page(PropagateNewsQuery query) {
        IPage<PropagateNewsEntity> page = baseMapper.selectPage(getPage(query), getWrapper(query));
        List<PropagateNewsVO> result = BeanUtil.copyToList(page.getRecords(), PropagateNewsVO.class);
        sysOrgService.setVisible(result);
        return new PageResult<>(result, page.getTotal());
    }

    private LambdaQueryWrapper<PropagateNewsEntity> getWrapper(PropagateNewsQuery query) {
        LambdaQueryWrapper<PropagateNewsEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StringUtils.isNotEmpty(query.getTitle()), PropagateNewsEntity::getTitle, query.getTitle())
                .eq(ObjectUtils.isNotEmpty(query.getContentType()), PropagateNewsEntity::getContentType, query.getContentType());
        // 数据权限
        //dataScopeWrapper(wrapper);
        return wrapper.orderByDesc(PropagateNewsEntity::getCreateTime);
    }

    @Override
    public void save(PropagateNewsVO vo) {
        PropagateNewsEntity entity = PropagateNewsConvert.INSTANCE.convert(vo);
        entity.setOrgId(SecurityUser.getUser().getOrgId());
        baseMapper.insert(entity);
    }

    @Override
    public void update(PropagateNewsVO vo) {
        PropagateNewsEntity entity = PropagateNewsConvert.INSTANCE.convert(vo);
        updateById(entity);
    }

    @Override
    public void unmount(Long id, Integer status) {
        PropagateNewsEntity entity = new PropagateNewsEntity();
        entity.setId(id);
        entity.setStatus(status);
        if (ApplyEnum.PUTAWAY.getStatus() == status) {
            entity.setAuditTime(new Date());
        }
        baseMapper.updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }

}